﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using pwblog.Entities;
using pwblog.DataAccess;

namespace pwblog.BusinessObject
{
    public class ActiveCodeBo
    {
        ActiveCodeDao activeCodeDao = new ActiveCodeDao();

        /// <summary>
        /// Get active code
        /// </summary>
        /// <param name="code">Active Code</param>
        /// <returns>ActiveCode</returns>
        public ActiveCode GetActiveCodeByCode(string code)
        {
            try
            {
                return activeCodeDao.GetActiveCodeByCode(code);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Get active code
        /// </summary>
        /// <param name="username">username</param>
        /// <returns>ActiveCode[]</returns>
        public ActiveCode[] GetActiveCodeByUsername(string username)
        {
            try
            {
                return activeCodeDao.GetActiveCodeByUsername(username);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Insert new active code for specific username
        /// </summary>
        internal string InsertActiveCode(string username)
        {
            Random random = new Random();
            string code = "";
            do
            {
                code = RandomString(10, random);
                if (activeCodeDao.GetActiveCodeByCode(code) == null)
                    break;
            } while (true);

            try
            {
                ActiveCode activeCode = new ActiveCode();
                activeCode.Code = code;
                activeCode.Username = username;
                activeCode.DateCreated = DateTime.Now;
                activeCode.IsExpired = 0;

                activeCodeDao.InsertActiveCode(activeCode);
                return code;
            }
            catch { return null; }
        }

        private string RandomString(int size, Random random)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

        /// <summary>
        /// delete active code
        /// </summary>
        /// 
        internal bool DeleteActiveCode(string code)
        {
            try
            {
                ActiveCode activeCode = activeCodeDao.GetActiveCodeByCode(code);
                if (activeCode != null)
                {
                    activeCodeDao.DeleteActiveCode(activeCode);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch { return false; }
        }

        /// <summary>
        /// expire active code
        /// </summary>
        /// 
        internal bool ExpiringCode(string code)
        {
            try
            {
                ActiveCode activeCode = activeCodeDao.GetActiveCodeByCode(code);
                if (activeCode != null)
                {
                    activeCode.IsExpired = 1;
                    activeCodeDao.UpdateActiveCode(activeCode);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch { return false; }
        }
    }
}